﻿namespace PreGUI.Widgets
{
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;
    using UnityEngine.EventSystems;

    /// <summary>
    /// PreGui Text Editor Control
    /// </summary>
    public class PGTextEdit : IPGControl
    {
        public string ControlName;

        private InputField _textEditor;
        private string _text;
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
                if (_textEditor == null)
                    return;
                _textEditor.text = value;
            }
        }
        public InputField.InputType InputType = InputField.InputType.Standard;
        public GameObject LayoutControls(PreGui gui, GameObject parent, string windowName)
        {
            var gameObject = new GameObject("PGTextEditor");
            gameObject.transform.SetParent(parent.transform, false);

            // Add the edit fixer to get around a bug with unity ui's caret
            gameObject.AddComponent<PGEditFixer>();

            var cursorListener = gameObject.AddComponent<PGCursorListener>();
            cursorListener.Gui = gui;
            cursorListener.CursorStyle = eCursorMode.Beam;

            if (gui.ViewModels.ContainsKey(windowName))
                this.BindTo(ControlName, gui.ViewModels[windowName]);

            var imageComponent = gameObject.AddComponent<Image>();
            imageComponent.color = gui.TextEditorSettings.BackgroundColor;
            imageComponent.sprite = gui.ControlSprite;
            imageComponent.type = Image.Type.Sliced;

            var textGameObject = new GameObject("Text");
            textGameObject.transform.SetParent(gameObject.transform, false);

            // 4,0  4,4
            var rectTextTransform = textGameObject.AddComponent<RectTransform>();
            rectTextTransform.anchorMin = new Vector2(0f, 0f);
            rectTextTransform.anchorMax = new Vector2(1f, 1f);
            rectTextTransform.position = gameObject.transform.position;

            rectTextTransform.offsetMin = new Vector2(8f, 4f);
            rectTextTransform.offsetMax = new Vector2(8f, 0f);

            var textComponent = textGameObject.AddComponent<Text>();
            textComponent.supportRichText = false;
            textComponent.font = gui.TextEditorSettings.Font;
            textComponent.fontSize = gui.TextEditorSettings.FontSize;
            textComponent.fontStyle = gui.TextEditorSettings.FontStyle;
            textComponent.color = gui.TextEditorSettings.FontColor;
            textComponent.text = Text;

            _textEditor = gameObject.AddComponent<InputField>();
            _textEditor.textComponent = textComponent;
            _textEditor.inputType = InputType;
            _textEditor.onValueChange.AddListener(newValue =>
            {
                _text = newValue;
                if (gui.ViewModels.ContainsKey(windowName))
                    gui.ViewModels[windowName].UpdateControlValue(ControlName, newValue);
            });

            var layoutElement = gameObject.AddComponent<LayoutElement>();
            layoutElement.minHeight = gui.TextEditorSettings.Height;

            return gameObject;
        }



        public IPGControl GenerateFromXmlNode(System.Xml.XmlNode node)
        {
            var inputType = (InputField.InputType)System.Enum.Parse(typeof(InputField.InputType), node.Attributes["InputType"].Value, true);

            Text = node.InnerText;
            InputType = inputType;


            if (node.Attributes["Name"] != null)
                ControlName = node.Attributes["Name"].Value;

            return this;
        }
    }
}
